package com.tadpole.music.view.fragment.question;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.question.SimulationAdapter;
import com.tadpole.music.bean.SystemConfigBean;
import com.tadpole.music.bean.question.SimulationBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.home.SystemConfigIView;
import com.tadpole.music.iView.question.SimulationIView;
import com.tadpole.music.presenter.home.SystemConfigPresenter;
import com.tadpole.music.presenter.question.SimulationPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.MemberCenterActivity;
import com.tadpole.music.view.activity.me.PayDetailActivity;
import com.tadpole.music.view.activity.question.SimulationActivity;
import com.tadpole.music.view.activity.question.SimulationQuestionActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;
import com.tadpole.music.view.widget.MyRecyclerViewDivider;
import com.tadpole.music.view.widget.PurchasePopup;

import java.util.List;

/**
 * 模拟套题
 * Created By 孙岩
 * on 2020/9/9
 */
public class SimulationHomeFragment extends BaseFragment implements SimulationIView, SystemConfigIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    //学院id
    private String mParam1;
    //等级id
    private String mParam2;
    private RecyclerView recyclerView;
    private SimulationAdapter simulationAdapter;
    private SmartRefreshLayout rl_refresh;
    //获取套题列表
    private SimulationPresenter simulationQuestionPresenter;
    //获取套题价格
    private SystemConfigPresenter systemConfigPresenter;
    //套题价格
    private String suit_price = "";

    public static SimulationHomeFragment newInstance(String param1, String param2) {
        SimulationHomeFragment fragment = new SimulationHomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_home_simulation;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView(View mRootView) {
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        rl_refresh = mRootView.findViewById(R.id.rl_refresh);

        simulationQuestionPresenter = new SimulationPresenter();
        simulationQuestionPresenter.attachView(this);
        simulationQuestionPresenter.getSimulation(String.valueOf(mParam1), String.valueOf(mParam2), false);

        systemConfigPresenter = new SystemConfigPresenter();
        systemConfigPresenter.attachView(this);
        systemConfigPresenter.getSystemConfig();
        initListeners();


    }

    //初始化监听器
    private void initListeners() {
        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                simulationQuestionPresenter.getSimulation(String.valueOf(mParam1), String.valueOf(mParam2), false);
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                simulationQuestionPresenter.getSimulationQuestionMore(String.valueOf(mParam1), String.valueOf(mParam2));
            }
        });
    }

    @Override
    public void showSimulationQuestion(final List<SimulationBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        simulationAdapter = new SimulationAdapter(getActivity(), 1, list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(simulationAdapter);
        recyclerView.setNestedScrollingEnabled(false);

        simulationAdapter.setOnChildClickListener(new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(View v, final int position) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        if (list.get(position).isPaid()) {//已支付
                            Intent intent = new Intent(getActivity(), SimulationQuestionActivity.class);
                            intent.putExtra("id",list.get(position).getId());
                            startActivity(intent);
                        } else {//未支付 去购买
                            final PurchasePopup popup = new PurchasePopup(getActivity(),suit_price);
                            popup.setPopupWindowFullScreen(true);
                            popup.showPopupWindow();
                            popup.setModeListener(new PurchasePopup.IModeSelection() {
                                @Override
                                public void getMode(int mode) {
                                    if (mode == 1) {//9.9
                                        Intent intent = new Intent(getActivity(), PayDetailActivity.class);
                                        intent.putExtra("name",list.get(position).getName());
                                        intent.putExtra("suit_id",String.valueOf(list.get(position).getId()));
                                        intent.putExtra("desc",list.get(position).getDesc());
                                        intent.putExtra("price",suit_price);
                                        intent.putExtra("flag","2");
                                        startActivity(intent);
                                        popup.dismiss();
                                    } else if (mode == 2) {//498
                                        Intent intent = new Intent(getActivity(), MemberCenterActivity.class);
                                        intent.putExtra("flag","1");
                                        intent.putExtra("college_id",String.valueOf(list.get(position).getCollege_id()));
                                        intent.putExtra("rank_id",String.valueOf(list.get(position).getRank_id()));
                                        startActivity(intent);
                                        popup.dismiss();
                                    }else if (mode == 3) {
                                        popup.dismiss();
                                    }
                                }
                            });
                        }
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void showNone() {

    }


    @Override
    public void notifyAdapter() {
        simulationAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
        rl_refresh.finishLoadMore();
    }

    @Override
    public void showSystemConfig(SystemConfigBean.DataBean bean) {
        suit_price = bean.getSuit_price();
    }
}
